package com.chenbei.controller.admin.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.chenbei.common.annotation.OperationLog;
import com.chenbei.common.domain.vo.TreeNode;
import com.chenbei.common.util.ApiResponseUtil;
import com.chenbei.common.domain.vo.ApiResponse;
import com.chenbei.system.domain.SysOrganization;
import com.chenbei.system.service.ISysOrganizationService;
import com.chenbei.system.vo.req.SysOrganizationReq;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "组织部门管理")
@RestController("AdminOrganizationController")
@RequestMapping("/admin/system/organization")
public class OrganizationController {

    @Autowired
    private ISysOrganizationService organizationService;

    @SaCheckPermission(value = "system:org:add", orRole = "root")
    @Operation(summary = "新增组织部门")
    @PostMapping("add")
    @OperationLog(moduleName = "组织部门管理", eventName = "添加组织部门")
    public ApiResponse<Object> add(@Valid @RequestBody SysOrganization organization) {
        organizationService.add(organization);
        return ApiResponseUtil.success();
    }

    @SaCheckPermission(value = "system:org:update", orRole = "root")
    @Operation(summary = "更新组织部门")
    @PostMapping("update")
    @OperationLog(moduleName = "组织部门管理", eventName = "更新组织部门")
    public ApiResponse<Object> update(@Valid @RequestBody SysOrganization organization) {
        organizationService.update(organization);
        return ApiResponseUtil.success();
    }

    @Operation(summary = "根据PID获取组织部门列表")
    @GetMapping("listByPid")
    public ApiResponse<List<SysOrganization>> listByPid(Long pid) {
        SysOrganizationReq sysOrganizationReq = new SysOrganizationReq();
        sysOrganizationReq.setStatus(-1);
        sysOrganizationReq.setPid(pid);
        return ApiResponseUtil.success(organizationService.list(sysOrganizationReq));
    }

    @SaCheckPermission(value = "system:org:list", orRole = "root")
    @Operation(summary = "获取所有组织部门树形结构")
    @GetMapping("allTree")
    public ApiResponse<List<TreeNode<SysOrganization>>> allTree() {
        return ApiResponseUtil.success(organizationService.allTree());
    }

    @SaCheckPermission(value = "system:org:delete", orRole = "root")
    @Operation(summary = "删除组织部门")
    @PostMapping("deleteById/{id}")
    @OperationLog(moduleName = "组织部门管理", eventName = "删除组织部门")
    public ApiResponse<Object> delete(@PathVariable Long id) {
        organizationService.delete(id);
        return ApiResponseUtil.success();
    }
}
